<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
</head>
<script type="text/javascript">
    var socket;

    if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
    }

    if (window.WebSocket) {
        //绑定端口9005 /ws为服务端设置路由
        socket = new WebSocket("ws://192.168.61.1:9005/ws");

        socket.onmessage = function (event) {
            console.log("接收到服务器消息:receive:" + event.data)
            appendln("接收到服务器消息:receive:" + event.data);
        };

        socket.onopen = function (event) {

            appendln("WebSocket is opened 建立连接.");
        };

        socket.onclose = function (event) {
            appendln("WebSocket is closed 断开连接");
        };
    } else {
        alert("当前浏览器不支持web socket");
    }

    function send(message) {
        console.log("message= ", message)
        if (!window.WebSocket) {
            return;
        }
        console.log("socket.readyState = ", socket.readyState)
        console.log("WebSocket.OPEN = ", WebSocket.OPEN)
        //socket.readyState ===1 是连接状态
        if (socket.readyState === WebSocket.OPEN) {
            socket.send(message);
            appendln("send:" + message);
        } else {
            alert("WebSocket is failed-----");
        }
    }

    function appendln(text) {
        var ta = document.getElementById('responseText');
        ta.value += text + "\r\n";
    }

    function clear() {
        var ta = document.getElementById('responseText');
        ta.value = "";
    }

</script>
<body>
<form onSubmit="return false;">
    <input type="text" name="message" value="hello"/>
    <br/><br/>
    <input type="button" value="send" onClick="send(this.form.message.value)"/>
    <hr/>
    <h3>chat</h3>
    <textarea id="responseText" style="width: 800px;height: 300px;"></textarea>
</form>
</body>
</html>